package top.zk123.chain.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.zk123.chain.bean.Institution;
import top.zk123.chain.bean.ReturnInfo;
import top.zk123.chain.exception.FileEmptyException;
import top.zk123.chain.exception.FileUploadException;
import top.zk123.chain.exception.RegisterFailedException;
import top.zk123.chain.exception.WrongFileTypeException;
import top.zk123.chain.service.InstitutionService;

import javax.annotation.Resource;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
 * Institution 接口层
 *
 * @author Ke Zhang
 * @since 1.0
 */
@RestController
public class InstitutionController {

    /**
     * 存储 Token的 Headers Key
     **/
    protected static final String AUTHORIZATION_HEADER = "Authorization";
    @Resource
    InstitutionService institutionService;

    /**
     * 用户登录
     *
     * @param institutionId 用户名
     * @param password      密码
     * @param response      响应
     * @return ReturnInfo
     */
    @PostMapping(value = "/login")
    public ReturnInfo login(@RequestParam(value = "username") String institutionId,
                            @RequestParam(value = "password") String password,
                            ServletResponse response) {
        Institution institution = new Institution(institutionId, password);
        ReturnInfo returnInfo = institutionService.login(institution);
        // 如果登录成功
        // 将 Token 写入到 Response 的 Header，方便前端刷新 Token 从 Header 取值
        if (returnInfo.getCode() == 200) {
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            httpServletResponse.setStatus(HttpServletResponse.SC_OK);
            httpServletResponse.addHeader(AUTHORIZATION_HEADER, (String) returnInfo.getData());
        }
        return returnInfo;
    }

    /**
     * @param username 用户名
     * @param password 密码
     * @param nickname 机构名
     * @param avatar   机构头像
     * @param proof    凭证
     * @return ReturnInfo
     * @throws RegisterFailedException 注册失败
     * @throws FileUploadException     文件上传失败
     * @throws WrongFileTypeException  不支持的文件类型
     * @throws FileEmptyException      文件为空
     */
    @PostMapping("/register")
    public ReturnInfo register(@RequestParam(value = "username") String username,
                               @RequestParam(value = "password") String password,
                               @RequestParam(value = "nickname") String nickname,
                               @RequestParam(value = "avatar") MultipartFile avatar,
                               @RequestParam(value = "proof") MultipartFile[] proof) throws
            RegisterFailedException, FileUploadException, WrongFileTypeException, FileEmptyException {
        return institutionService.register(username, password, nickname, avatar, proof);
    }

    /**
     * 测试接口
     *
     * @return String
     */
    @GetMapping("/hello")
    public String hello() {
        return "已经登录的用户可见";
    }

    /**
     * 设置昵称
     *
     * @param nickname 昵称
     * @return ReturnInfo
     */
    @PostMapping("/nickname")
    public ReturnInfo setNickname(@RequestParam(value = "nickname") String nickname) {
        return institutionService.setNickname(nickname);
    }

    /**
     * 设置头像
     *
     * @param avatar 图片
     * @return ReturnInfo
     * @throws FileUploadException    文件上传失败
     * @throws WrongFileTypeException 不支持的文件类型
     * @throws FileEmptyException     文件为空
     */
    @PostMapping("/avatar")
    public ReturnInfo SetAvatar(@RequestParam(value = "avatar") MultipartFile avatar) throws
            FileUploadException, WrongFileTypeException, FileEmptyException {
        return institutionService.setAvatar(avatar);
    }
}
